#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_CONTACTS 100
#define NAME_LEN 50
#define TITLE_LEN 50
#define POSITION_LEN 50
#define PHONE_LEN 20
#define EMAIL_LEN 50

typedef struct {
	char name[NAME_LEN];
	char title[TITLE_LEN];
	char position[POSITION_LEN];
	char phone[PHONE_LEN];
	char email[EMAIL_LEN];
} Contact;

Contact contacts[MAX_CONTACTS];
int contact_count = 0;

// 函数声明
void add_contact();
void delete_contact();
void modify_contact();
void display_contacts();
void search_contacts();
void sort_contacts();
void save_contacts_to_file();
void load_contacts_from_file();

int main() {
	int choice;
	
	// 加载文件中的联系人
	load_contacts_from_file();
	
	while (1) {
		printf("\n通讯录管理系统\n");
		printf("1. 添加联系人\n");
		printf("2. 删除联系人\n");
		printf("3. 修改联系人\n");
		printf("4. 显示所有联系人\n");
		printf("5. 查找联系人\n");
		printf("6. 按姓名排序\n");
		printf("7. 退出\n");
		printf("请输入您的选择：");
		scanf("%d", &choice);
		
		switch (choice) {
		case 1:
			add_contact();
			break;
		case 2:
			delete_contact();
			break;
		case 3:
			modify_contact();
			break;
		case 4:
			display_contacts();
			break;
		case 5:
			search_contacts();
			break;
		case 6:
			sort_contacts();
			display_contacts();
			break;
		case 7:
			save_contacts_to_file();
			printf("退出程序。\n");
			exit(0);
		default:
			printf("无效的选择，请重新输入。\n");
		}
	}
	
	return 0;
}

void add_contact() {
	if (contact_count >= MAX_CONTACTS) {
		printf("通讯录已满，无法添加更多联系人。\n");
		return;
	}
	
	printf("请输入姓名：");
	scanf("%s", contacts[contact_count].name);
	printf("请输入职务：");
	scanf("%s", contacts[contact_count].title);
	printf("请输入职称：");
	scanf("%s", contacts[contact_count].position);
	printf("请输入手机号：");
	scanf("%s", contacts[contact_count].phone);
	printf("请输入Email：");
	scanf("%s", contacts[contact_count].email);
	
	contact_count++;
	printf("联系人添加成功。\n");
}

void delete_contact() {
	char name[NAME_LEN];
	printf("请输入要删除的联系人姓名：");
	scanf("%s", name);
	
	for (int i = 0; i < contact_count; i++) {
		if (strcmp(contacts[i].name, name) == 0) {
			for (int j = i; j < contact_count - 1; j++) {
				contacts[j] = contacts[j + 1];
			}
			contact_count--;
			printf("联系人删除成功。\n");
			return;
		}
	}
	printf("未找到该联系人。\n");
}

void modify_contact() {
	char name[NAME_LEN];
	printf("请输入要修改的联系人姓名：");
	scanf("%s", name);
	
	for (int i = 0; i < contact_count; i++) {
		if (strcmp(contacts[i].name, name) == 0) {
			printf("请输入新的职务：");
			scanf("%s", contacts[i].title);
			printf("请输入新的职称：");
			scanf("%s", contacts[i].position);
			printf("请输入新的手机号：");
			scanf("%s", contacts[i].phone);
			printf("请输入新的Email：");
			scanf("%s", contacts[i].email);
			printf("联系人修改成功。\n");
			return;
		}
	}
	printf("未找到该联系人。\n");
}

void display_contacts() {
	printf("\n联系人列表：\n");
    if(contact_count==0)
        printf("无");
	for (int i = 0; i < contact_count; i++) {
		printf("姓名：%s\n", contacts[i].name);
		printf("职务：%s\n", contacts[i].title);
		printf("职称：%s\n", contacts[i].position);
		printf("手机：%s\n", contacts[i].phone);
		printf("Email：%s\n", contacts[i].email);
		printf("-----------------------------\n");
	}
}

void search_contacts() {
	char keyword[100];
	printf("请输入查找关键字：");
	scanf("%s", keyword);
	
	printf("\n查找结果：\n");
	for (int i = 0; i < contact_count; i++) {
		if (strstr(contacts[i].name, keyword) || strstr(contacts[i].title, keyword) ||
			strstr(contacts[i].position, keyword) || strstr(contacts[i].phone, keyword) ||
			strstr(contacts[i].email, keyword)) {
			printf("姓名：%s\n", contacts[i].name);
			printf("职务：%s\n", contacts[i].title);
			printf("职称：%s\n", contacts[i].position);
			printf("手机：%s\n", contacts[i].phone);
			printf("Email：%s\n", contacts[i].email);
			printf("-----------------------------\n");
		}
	}
}

void sort_contacts() {
	for (int i = 0; i < contact_count - 1; i++) {
		for (int j = 0; j < contact_count - i - 1; j++) {
			if (strcmp(contacts[j].name, contacts[j + 1].name) > 0) {
				Contact temp = contacts[j];
				contacts[j] = contacts[j + 1];
				contacts[j + 1] = temp;
			}
		}
	}
	printf("联系人已按姓名排序。\n");
}

void save_contacts_to_file() {
	FILE *file = fopen("contacts.txt", "w");
	if (file == NULL) {
		printf("无法打开文件进行写入。\n");
		return;
	}
	
	for (int i = 0; i < contact_count; i++) {
		fprintf(file, "%s %s %s %s %s\n", contacts[i].name, contacts[i].title, contacts[i].position, contacts[i].phone, contacts[i].email);
	}
	
	fclose(file);
	printf("联系人已保存到文件。\n");
}

void load_contacts_from_file() {
	FILE *file = fopen("contacts.txt", "r");
	if (file == NULL) {
		printf("无法打开文件进行读取，可能是文件不存在。\n");
		return;
	}
	
	contact_count = 0;
	while (fscanf(file, "%s %s %s %s %s", contacts[contact_count].name, contacts[contact_count].title,
		contacts[contact_count].position, contacts[contact_count].phone, contacts[contact_count].email) != EOF) {
		contact_count++;
	}
	
	fclose(file);
	printf("联系人已从文件加载。\n");
}
